// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

import { ScrollView } from "std-widgets-impl.slint";

export component TextEditBase inherits Rectangle {
    in property <length> scroll-view-padding;

    in property <TextWrap> wrap <=> text-input.wrap;
    in property <TextHorizontalAlignment> horizontal-alignment <=> text-input.horizontal-alignment;
    in property <bool> read-only <=> text-input.read-only;
    in property <length> font-size <=> text-input.font-size;
    in property <bool> enabled <=> text-input.enabled;
    in-out property <bool> has-focus: text-input.has-focus;
    out property <length> visible-width <=> scroll-view.visible-width;
    out property <length> visible-height <=> scroll-view.visible-height;
    in-out property <string> text <=> text-input.text;
    in-out property <length> viewport-x <=> scroll-view.viewport-x;
    in-out property <length> viewport-y <=> scroll-view.viewport-y;
    in-out property <length> viewport-width <=> scroll-view.viewport-width;
    in-out property <length> viewport-height <=> scroll-view.viewport-height;

    in property <brush> foreground <=> text-input.color;
    in property <int> font-weight <=> text-input.font-weight;
    in property <brush> selection-background-color;
    in property <brush> selection-foreground-color;

    in property <string> placeholder-text;
    in property <brush> placeholder-color;

    callback edited(text: string);
    callback key-pressed(event: KeyEvent) -> EventResult;
    callback key-released(event: KeyEvent) -> EventResult;

    public function set-selection-offsets(start: int, end: int) {
        text-input.set-selection-offsets(start, end);
    }

    public function select-all() {
        text-input.select-all();
    }

    public function clear-selection() {
        text-input.clear-selection();
    }

    public function cut() {
        text-input.cut();
    }

    public function copy() {
        text-input.copy();
    }

    public function paste() {
        text-input.paste();
    }

    forward-focus: text-input;

    ContextMenuArea {
        enabled: root.enabled;
        Menu {
            MenuItem {
                title: @tr("Cut");
                enabled: !root.read-only && root.enabled;
                activated => {
                    text-input.cut();
                }
            }

            MenuItem {
                title: @tr("Copy");
                enabled: !root.text.is-empty;
                activated => {
                    text-input.copy();
                }
            }

            MenuItem {
                enabled: !root.read-only && root.enabled;
                title: @tr("Paste");
                activated => {
                    text-input.paste();
                }
            }

            MenuItem {
                title: @tr("Select All");
                enabled: !root.text.is-empty;
                activated => {
                    text-input.select-all();
                }
            }
        }

        scroll-view := ScrollView {
            x: root.scroll-view-padding;
            y: root.scroll-view-padding;
            width: parent.width - 2 * root.scroll-view-padding;
            height: parent.height - 2 * root.scroll-view-padding;
            viewport-width: root.wrap == TextWrap.no-wrap ? max(self.visible-width, text-input.preferred-width) : self.visible-width;
            viewport-height: max(self.visible-height, text-input.preferred-height);

            text-input := TextInput {
                enabled: true;
                single-line: false;
                wrap: word-wrap;
                selection-background-color: root.selection-background-color;
                selection-foreground-color: root.selection-foreground-color;
                page-height: scroll-view.visible-height;
                // Disable TextInput's built-in accessibility support as the widget takes care of that.
                accessible-role: none;

                edited => {
                    root.edited(self.text);
                }

                key-pressed(event) => {
                    root.key-pressed(event)
                }

                key-released(event) => {
                    root.key-released(event)
                }

                cursor-position-changed(cpos) => {
                    if (cpos.x + root.viewport-x < 12px) {
                        root.viewport-x = min(0px, max(parent.visible-width - self.width,  - cpos.x + 12px));
                    } else if (cpos.x + root.viewport-x > parent.visible-width - 12px) {
                        root.viewport-x = min(0px, max(parent.visible-width - self.width,  parent.visible-width - cpos.x - 12px));
                    }
                    if (cpos.y + root.viewport-y < 12px) {
                        root.viewport-y = min(0px, max(parent.visible-height - self.height,  - cpos.y + 12px));
                    } else if (cpos.y + root.viewport-y > parent.visible-height - 12px - 20px) {
                        // FIXME: font-height hardcoded to 20px
                            root.viewport-y = min(0px, max(parent.visible-height - self.height,  parent.visible-height - cpos.y - 12px - 20px));
                    }
                }
            }
        }
    }

    placeholder := Text {
        x: scroll-view.x;
        y: scroll-view.y;
        width: scroll-view.width;
        vertical-alignment: top;
        text: (root.text == "" && text-input.preedit-text == "") ? root.placeholder-text : "";
        font-size: text-input.font-size;
        font-italic: text-input.font-italic;
        font-weight: text-input.font-weight;
        font-family: text-input.font-family;
        color: root.placeholder-color;
        overflow: elide;
        // `accessible-placeholder-text` is set on TextEdit already
        accessible-role: none;
    }

    @children
}
